package com.vlin.util.app.result;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.json.JSONObject;

import com.vlin.entity.app.result.Result;
import com.vlin.errorcode.ErrorCode;

public class ResultUtils {

	public static String result(ErrorCode code) {
		
		return new JSONObject(Result.instance().setCode(code.getCode())
				  .setMsg(code.getMsg()) .setSuccess(false)
				  .setT(System.currentTimeMillis())).toString();
	}
	
	public static String result(Object result) {
		if(result == null) {
			return null;
		}
		
		return new JSONObject(Result.instance().setCode(0)
				  .setMsg("success") .setSuccess(true)
				  .setT(System.currentTimeMillis()).setResult(result)).toString();
	}
	
	public static String MD5Encode(String value) throws NoSuchAlgorithmException {
		MessageDigest md = MessageDigest.getInstance("MD5");
		md.update(value.getBytes());
		String str = new BigInteger(1, md.digest()).toString(16);
		return str;
	}
	
}
